package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.manage.common.core.mybatisplus.core.ServicePlusImpl;
import com.manage.common.utils.BeanCopyUtils;
import com.manage.modules.dto.TBaseStationSelectDto;
import com.manage.modules.dto.TBaseStationSelectReturnDto;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseStationInfoAddBo;
import com.manage.modules.bo.TBaseStationInfoQueryBo;
import com.manage.modules.bo.TBaseStationInfoEditBo;
import com.manage.modules.domain.TBaseStationInfo;
import com.manage.modules.mapper.TBaseStationInfoMapper;
import com.manage.modules.vo.TBaseStationInfoVo;
import com.manage.modules.service.ITBaseStationInfoService;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 站点信息Service业务层处理
 *
 * @author wx
 * @date 2021-08-11
 */
@Service
public class TBaseStationInfoServiceImpl extends ServicePlusImpl<TBaseStationInfoMapper, TBaseStationInfo, TBaseStationInfoVo> implements ITBaseStationInfoService {
    @Override
    public List<TBaseStationSelectReturnDto> queryBySelectDto(TBaseStationSelectDto selectDto) {
        LambdaQueryWrapper<TBaseStationInfo> lqw = Wrappers.lambdaQuery();
        if (selectDto != null) {
            lqw
                    .eq(selectDto.getStationState() != null, TBaseStationInfo::getStationState, selectDto.getStationState())
                    .like(StrUtil.isNotBlank(selectDto.getKeyword()), TBaseStationInfo::getStationName, selectDto.getKeyword())
                    .or()
                    .like(StrUtil.isNotBlank(selectDto.getKeyword()), TBaseStationInfo::getStationIdC, selectDto.getKeyword())
                    .select(TBaseStationInfo::getStationId, TBaseStationInfo::getStationName, TBaseStationInfo::getStationIdC, TBaseStationInfo::getLat, TBaseStationInfo::getLon);
        }
        List<TBaseStationInfo> list = list(lqw);

        return BeanCopyUtils.listCopy(list, CopyOptions.create(), TBaseStationSelectReturnDto.class);
    }

    @Override
    public void saveListStation(List<TBaseStationInfoAddBo> stationInfoAddBoList) {
        stationInfoAddBoList.forEach(tBaseStationInfoAddBo -> {
            tBaseStationInfoAddBo.setStationState(1L);
            insertByAddBo(tBaseStationInfoAddBo);
        });
    }

    @Override
    public TBaseStationInfoVo queryById(Long stationId) {
        TBaseStationInfo db = this.baseMapper.selectById(stationId);
        return BeanUtil.toBean(db, TBaseStationInfoVo.class);
    }

    @Override
    public List<TBaseStationInfoVo> queryList(TBaseStationInfoQueryBo bo) {
        LambdaQueryWrapper<TBaseStationInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStationTypeId() != null, TBaseStationInfo::getStationTypeId, bo.getStationTypeId());
        lqw.eq(bo.getAreaId() != null, TBaseStationInfo::getAreaId, bo.getAreaId());
        lqw.like(StrUtil.isNotBlank(bo.getStationName()), TBaseStationInfo::getStationName, bo.getStationName());
        lqw.eq(bo.getStationState() != null, TBaseStationInfo::getStationState, bo.getStationState());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), TBaseStationInfo::getPhone, bo.getPhone());
        lqw.like(StrUtil.isNotBlank(bo.getContactPeopleName()), TBaseStationInfo::getContactPeopleName, bo.getContactPeopleName());
        lqw.eq(StrUtil.isNotBlank(bo.getCity()), TBaseStationInfo::getCity, bo.getCity());
        lqw.eq(StrUtil.isNotBlank(bo.getStationLevel()), TBaseStationInfo::getStationLevel, bo.getStationLevel());
        lqw.eq(StrUtil.isNotBlank(bo.getAdminCodeChn()), TBaseStationInfo::getAdminCodeChn, bo.getAdminCodeChn());
        lqw.eq(StrUtil.isNotBlank(bo.getVAcode4search()), TBaseStationInfo::getVAcode4search, bo.getVAcode4search());
        lqw.eq(StrUtil.isNotBlank(bo.getTown()), TBaseStationInfo::getTown, bo.getTown());
        lqw.eq(StrUtil.isNotBlank(bo.getCountryCode()), TBaseStationInfo::getCountryCode, bo.getCountryCode());
        lqw.eq(StrUtil.isNotBlank(bo.getProvince()), TBaseStationInfo::getProvince, bo.getProvince());
        lqw.like(StrUtil.isNotBlank(bo.getRegionCode()), TBaseStationInfo::getRegionCode, bo.getRegionCode());
        lqw.eq(StrUtil.isNotBlank(bo.getNetCode()), TBaseStationInfo::getNetCode, bo.getNetCode());
        lqw.eq(StrUtil.isNotBlank(bo.getCountry()), TBaseStationInfo::getCountry, bo.getCountry());
        lqw.eq(StrUtil.isNotBlank(bo.getCnty()), TBaseStationInfo::getCnty, bo.getCnty());
        lqw.like(StrUtil.isNotBlank(bo.getStationIdC()), TBaseStationInfo::getStationIdC, bo.getStationIdC());
        lqw.eq(StrUtil.isNotBlank(bo.getLat()), TBaseStationInfo::getLat, bo.getLat());
        lqw.eq(StrUtil.isNotBlank(bo.getLon()), TBaseStationInfo::getLon, bo.getLon());
        lqw.eq(StrUtil.isNotBlank(bo.getAlti()), TBaseStationInfo::getAlti, bo.getAlti());
        lqw.eq(StrUtil.isNotBlank(bo.getTownCode()), TBaseStationInfo::getTownCode, bo.getTownCode());
        lqw.eq(StrUtil.isNotBlank(bo.getMonitorTimeInterval()), TBaseStationInfo::getMonitorTimeInterval, bo.getMonitorTimeInterval());
        lqw.eq(StrUtil.isNotBlank(bo.getMaintainTime()), TBaseStationInfo::getMaintainTime, bo.getMaintainTime());
        lqw.eq(StrUtil.isNotBlank(bo.getParentCode()), TBaseStationInfo::getParentCode, bo.getParentCode());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseStationInfoVo> entity2Vo(Collection<TBaseStationInfo> collection) {
        List<TBaseStationInfoVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseStationInfoVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseStationInfo> page = (Page<TBaseStationInfo>) collection;
            Page<TBaseStationInfoVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseStationInfoAddBo bo) {
        TBaseStationInfo add = BeanUtil.toBean(bo, TBaseStationInfo.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseStationInfoEditBo bo) {
        TBaseStationInfo update = BeanUtil.toBean(bo, TBaseStationInfo.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseStationInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
